// DX10playground.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "vld.h"

#include "MengineApp.h"
#include "SceneView.h"
#include "Core.h"
#include "ViewManager.h"
#include "Scene.h"
#include "GraphicsResources.h"
#include "MeshManager.h"
#include "ModelManager.h"
#include "ModelNode.h"
#include "TextureManager.h"
#include "Model.h"
#include "ShaderManager.h"
#include "MaterialManager.h"
#include "Material.h"

class TestAppSceneView : public mcore::SceneView
{
public:
	TestAppSceneView()
		: mcore::SceneView("TestAppSceneView")
	{
	}

	virtual void load()
	{
		GraphicsResources * res = GraphicsResources::getGlobal();

		mcore::SceneView::load();
		//mScene->addModelNode(new ModelNode(GraphicsResources::getGlobal()->getModelMgr()->getDefaultModel()));

		Model * model = res->getModelMgr()->createModel("SphereWithNormal");
		model->setMesh(res->getMeshMgr()->getMesh("Sphere"));
		Material * mat = res->getMaterialMgr()->createMaterial("floor");
		mat->DiffuseMap = res->getTextureMgr()->createTex(L"content\\textures\\floor2.jpg");
		mat->NormalMap = res->getTextureMgr()->createTex(L"content\\textures\\floor2_Normal.jpg");
		mat->Shader = res->getShaderMgr()->getShader(mgfx::NORMAL_1PL);
		mat->SpecularColor = XMCOLOR4F(0.7f, 0.7f, 0.7f, 8.0f);
		mat->DiffuseColor = XMCOLOR4F(1.0f, 1.0f, 1.0f, 1.0f);
		model->setMaterial(mat);

		/*Model * model3 = res->getModelMgr()->createModel("BoxWithNormal");
		model3->setMesh(res->getMeshMgr()->getMesh("Box"));
		mat = res->getMaterialMgr()->createMaterial("sth");
		mat->DiffuseMap = res->getTextureMgr()->createTex(L"content\\textures\\6147.jpg");
		mat->NormalMap = res->getTextureMgr()->createTex(L"content\\textures\\6147-normal.jpg");
		mat->Shader = res->getShaderMgr()->getShader(mgfx::NORMAL_1PL);
		mat->SpecularColor = XMCOLOR4F(0.7f, 0.7f, 0.7f, 8.0f);
		mat->DiffuseColor = XMCOLOR4F(1.0f, 1.0f, 1.0f, 1.0f);
		model3->setMaterial(mat);*/

		Model * model2 = res->getModelMgr()->createModel("QuadWithNormal");
		model2->setMesh(res->getMeshMgr()->getMesh("Quad"));
		mat = res->getMaterialMgr()->createMaterial("brickwork");
		mat->DiffuseMap = res->getTextureMgr()->createTex(L"content\\textures\\brickwork-texture.jpg");
		mat->NormalMap = res->getTextureMgr()->createTex(L"content\\textures\\brickwork_normal-map.jpg");
		mat->Shader = res->getShaderMgr()->getShader(mgfx::NORMAL_1PL);
		mat->SpecularColor = XMCOLOR4F(1.0f, 1.0f, 1.0f, 8.0f);
		mat->DiffuseColor = XMCOLOR4F(1.0f, 0.8f, 0.8f, 1.0f);
		model2->setMaterial(mat);

		ModelNode * node = new ModelNode(model2);
		node->setPosition(XMFLOAT3(50, -2.5f, 50));
		mScene->addModelNode(node);

		bool b = true;

		for(uint i=0 ; i<10 ; i+=3)
			for(uint j=0 ; j<10 ; j+=3)
			{
				if (j == 5 && i== 5)
					continue;
				//if(b)
					node = new ModelNode(model);
				//else
				//	node = new ModelNode(model3);
				node->setPosition(XMFLOAT3(10*i, 0, 10*j));
				mScene->addModelNode(node);
				b = !b;
			}
	}
};

class TestApp : public mcore::MengineApp
{
public:
	TestApp(HINSTANCE hInstance)
		: MengineApp(hInstance)
	{

	}

	virtual ~TestApp(){}

	virtual void userInit()
	{
		mCore.getViewManager()->setActiveView(new TestAppSceneView);
	}
};

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
				   PSTR cmdLine, int showCmd)
{
	// Enable run-time memory check for debug builds.
#if defined(DEBUG) | defined(_DEBUG)
	//_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif

	TestApp app(hInstance);
	app.init();
	return app.run();

	/*Framework theApp(hInstance, new TestView);
	theApp.initApp();

	return theApp.run();*/
}
